home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / Encoders.py < prev    next >
Text File  |  2005-11-19  |  3KB  |  95 lines

  1. # Copyright (C) 2001,2002 Python Software Foundation
  2. # Author: barry@zope.com (Barry Warsaw)
  3.  
  4. """Module containing encoding functions for Image.Image and Text.Text.
  5. """
  6.  
  7. import base64
  8.  
  9.  
  10.  
  11. # Helpers
  12. try:
  13.     from quopri import encodestring as _encodestring
  14.  
  15.     def _qencode(s):
  16.         enc = _encodestring(s, quotetabs=1)
  17.         # Must encode spaces, which quopri.encodestring() doesn't do
  18.         return enc.replace(' ', '=20')
  19. except ImportError:
  20.     # Python 2.1 doesn't have quopri.encodestring()
  21.     from cStringIO import StringIO
  22.     import quopri as _quopri
  23.  
  24.     def _qencode(s):
  25.         if not s:
  26.             return s
  27.         hasnewline = (s[-1] == '\n')
  28.         infp = StringIO(s)
  29.         outfp = StringIO()
  30.         _quopri.encode(infp, outfp, quotetabs=1)
  31.         # Python 2.x's encode() doesn't encode spaces even when quotetabs==1
  32.         value = outfp.getvalue().replace(' ', '=20')
  33.         if not hasnewline and value[-1] == '\n':
  34.             return value[:-1]
  35.         return value
  36.  
  37.  
  38. def _bencode(s):
  39.     # We can't quite use base64.encodestring() since it tacks on a "courtesy
  40.     # newline".  Blech!
  41.     if not s:
  42.         return s
  43.     hasnewline = (s[-1] == '\n')
  44.     value = base64.encodestring(s)
  45.     if not hasnewline and value[-1] == '\n':
  46.         return value[:-1]
  47.     return value
  48.  
  49.  
  50.  
  51. def encode_base64(msg):
  52.     """Encode the message's payload in Base64.
  53.  
  54.     Also, add an appropriate Content-Transfer-Encoding header.
  55.     """
  56.     orig = msg.get_payload()
  57.     encdata = _bencode(orig)
  58.     msg.set_payload(encdata)
  59.     msg['Content-Transfer-Encoding'] = 'base64'
  60.  
  61.  
  62.  
  63. def encode_quopri(msg):
  64.     """Encode the message's payload in quoted-printable.
  65.  
  66.     Also, add an appropriate Content-Transfer-Encoding header.
  67.     """
  68.     orig = msg.get_payload()
  69.     encdata = _qencode(orig)
  70.     msg.set_payload(encdata)
  71.     msg['Content-Transfer-Encoding'] = 'quoted-printable'
  72.  
  73.  
  74.  
  75. def encode_7or8bit(msg):
  76.     """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
  77.     orig = msg.get_payload()
  78.     if orig is None:
  79.         # There's no payload.  For backwards compatibility we use 7bit
  80.         msg['Content-Transfer-Encoding'] = '7bit'
  81.         return
  82.     # We play a trick to make this go fast.  If encoding to ASCII succeeds, we
  83.     # know the data must be 7bit, otherwise treat it as 8bit.
  84.     try:
  85.         orig.encode('ascii')
  86.     except UnicodeError:
  87.         msg['Content-Transfer-Encoding'] = '8bit'
  88.     else:
  89.         msg['Content-Transfer-Encoding'] = '7bit'
  90.  
  91.  
  92.  
  93. def encode_noop(msg):
  94.     """Do nothing."""
  95.